package com.flink.hbase;

import org.apache.flink.types.Row;

import java.io.Serializable;
import java.util.Objects;

/**
 * User 数据模型类
 * 对应 HBase user 表的数据结构
 */
public class User implements Serializable {
    
    private String userId;
    private String name;
    private Integer age;
    private String gender;
    private Long ts;  // 业务时间戳
    private String dt; // 分区日期，格式如 "2023-12-01"

    public User() {
    }

    public User(String userId, String name, Integer age, String gender, Long ts, String dt) {
        this.userId = userId;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.ts = ts;
        this.dt = dt;
    }

    // Getters and Setters
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Long getTs() {
        return ts;
    }

    public void setTs(Long ts) {
        this.ts = ts;
    }

    public String getDt() {
        return dt;
    }

    public void setDt(String dt) {
        this.dt = dt;
    }

    /**
     * 转换为 Flink Row 对象
     */
    public Row toRowData() {
        return Row.of(userId, name, age, gender, ts, dt);
    }

    /**
     * 从 Flink Row 对象创建 User
     */
    public static User fromRowData(Row row) {
        return new User(
            row.getFieldAs(0),
            row.getFieldAs(1),
            row.getFieldAs(2),
            row.getFieldAs(3),
            row.getFieldAs(4),
            row.getFieldAs(5)
        );
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(userId, user.userId) &&
               Objects.equals(name, user.name) &&
               Objects.equals(age, user.age) &&
               Objects.equals(gender, user.gender) &&
               Objects.equals(ts, user.ts) &&
               Objects.equals(dt, user.dt);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, name, age, gender, ts, dt);
    }

    @Override
    public String toString() {
        return "User{" +
               "userId='" + userId + '\'' +
               ", name='" + name + '\'' +
               ", age=" + age +
               ", gender='" + gender + '\'' +
               ", ts=" + ts +
               ", dt='" + dt + '\'' +
               '}';
    }
} 